using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
namespace MemoryGame
{
    public class StateWinLose : DEF
    {
        public static Rectangle rectF;
        public static int MENU_H = SCREEN_HEIGHT;
        public static int m_start = 0; public static int totalScore = 0;
        public static bool isWin = false;
        public static Texture2D winEffect;
        public static Texture2D loseEffect;

        public static void SendMessage(int type)
        {
            switch (type)
            {
                case MESSAGE_CTOR:
                    if (loseEffect == null)

                        loseEffect = Game1.contentManager.Load<Texture2D>("image//loseanim");
                    if (winEffect == null)
                        winEffect = Game1.contentManager.Load<Texture2D>("image//winanim");
                    DEF.WINLOSE_CONTENT_SPACE_H = (int)Game1.fontNormal.MeasureString("AHGj").Y;//fontsmall_White.getFontHeight();
                    if (StateWinLose.isWin)
                    {
                        Game1.mLevelUnlock++;
                        Game1.instanceGame1.saveLevelUnlock();
                    }
                    SoundManager.pausesoundLoop(1);
                    if (isWin)
                        SoundManager.playSound(SoundManager.SOUND_WIN);
                    else
                        SoundManager.playSound(SoundManager.SOUND_LOSE);

                    //Log.d("USER NAME", "User : "+ username);
                    DEF.WINLOSE_BACKGROUND_W = SCREEN_WIDTH;
                    DEF.WINLOSE_BACKGROUND_H = SCREEN_HEIGHT;
                    DEF.WINLOSE_BACKGROUND_X = 0;
                    DEF.WINLOSE_BACKGROUND_Y = 0;

                    rectF = new Rectangle(DEF.WINLOSE_BACKGROUND_X, DEF.WINLOSE_BACKGROUND_Y, DEF.WINLOSE_BACKGROUND_X + DEF.WINLOSE_BACKGROUND_W, DEF.WINLOSE_BACKGROUND_Y + DEF.WINLOSE_BACKGROUND_H);
                    DEF.WINLOSE_TITLE_Y = DEF.WINLOSE_BACKGROUND_Y + 1;
                    DEF.WINLOSE_CONTENT_Y = DEF.WINLOSE_TITLE_Y + 2 * DEF.WINLOSE_CONTENT_SPACE_H;

                    DEF.WINLOSE_BUTTON_X2 = SCREEN_WIDTH / 2 - DEF.DIALOG_BUTTON_CONFIRM_H / 2;
                    DEF.WINLOSE_BUTTON_X1 = DEF.WINLOSE_BUTTON_X2 - 2 * DEF.DIALOG_BUTTON_CONFIRM_W;

                    DEF.WINLOSE_BUTTON_X3 = DEF.WINLOSE_BUTTON_X2 + 2 * DEF.DIALOG_BUTTON_CONFIRM_W;

                    DEF.WINLOSE_BUTTON_Y1 = DEF.WINLOSE_BACKGROUND_Y + DEF.WINLOSE_BACKGROUND_H - 2 * DEF.DIALOG_BUTTON_CONFIRM_H;
                    DEF.WINLOSE_BUTTON_Y2 = DEF.WINLOSE_BUTTON_Y3 = DEF.WINLOSE_BUTTON_Y1;
                    break;
                case MESSAGE_UPDATE:
                    if (Game1.frameCountCurrentState < 20)
                        return;


                    if (InputState.isTouchReleaseInRect(DEF.WINLOSE_BUTTON_X1, DEF.WINLOSE_BUTTON_Y1, DEF.DIALOG_BUTTON_CONFIRM_W, DEF.DIALOG_BUTTON_CONFIRM_H))
                        Game1.changeState(Game1.STATE_GAMEPLAY);

                    if (InputState.isBackKeyPress() || InputState.isTouchReleaseInRect(DEF.WINLOSE_BUTTON_X2, DEF.WINLOSE_BUTTON_Y2, DEF.DIALOG_BUTTON_CONFIRM_W, DEF.DIALOG_BUTTON_CONFIRM_H))
                        Game1.changeState(Game1.STATE_MAINMENU);

                    break;
                case MESSAGE_PAINT:
                    StateGameplay.SendMessage(MESSAGE_PAINT);
                    if (Dialog.isShowDialog)
                    {
                        Dialog.drawDialog(Game1.spriteBatch);
                        return;
                    }
                    var t = new Texture2D(Game1.graphicsDevice, 1, 1);
                    t.SetData(new[] { Color.White });
                    Game1.spriteBatch.Draw(t, rectF, new Color(0, 0, 0, 220));

                    Vector2 tempMeasure;// = Game1.fontNormal.MeasureString("YOU LOSE");
                    float scaleMaster = SCREEN_WIDTH / 800f;
                    float scale = (1.0f * Game1.frameCountCurrentState) / 20;
                    scale = scale * scaleMaster;
                    if (scale > scaleMaster)
                        scale = scaleMaster;

                    if (isWin)
                    {
                        Game1.spriteBatch.Draw(winEffect, new Vector2((SCREEN_WIDTH - scale * winEffect.Width) / 2, (SCREEN_HEIGHT - scale * winEffect.Height) / 2), null, Color.White, 0f, new Vector2(0, 0), new Vector2(scale, scale), SpriteEffects.None, 1);


                    }
                    else
                    {
                        Game1.spriteBatch.Draw(loseEffect, new Vector2((SCREEN_WIDTH - scale * loseEffect.Width) / 2, (SCREEN_HEIGHT - scale * loseEffect.Height) / 2), null, Color.White, 0f, new Vector2(0, 0), new Vector2(scale, scale), SpriteEffects.None, 1);

                    }



                    tempMeasure = Game1.fontNormal.MeasureString("Finish");
                    Game1.spriteBatch.DrawString(Game1.fontNormal, "Finish", new Vector2(DEF.SCREEN_WIDTH / 2 - tempMeasure.X / 2, DEF.SELECTLEVEL_BACKGROUND_Y - 2), Color.Blue);


                    if (InputState.isTouchDrapInRect(DEF.WINLOSE_BUTTON_X1, DEF.WINLOSE_BUTTON_Y1, DEF.DIALOG_BUTTON_CONFIRM_W, DEF.DIALOG_BUTTON_CONFIRM_H))

                        Game1.spriteBatch.Draw(texttureButtonMenu[DEF.FRAME_RETRY_NORMAL], new Vector2(DEF.WINLOSE_BUTTON_X1, DEF.WINLOSE_BUTTON_Y1), null, Color.White, 0f, new Vector2(0, 0), new Vector2(Game1.SCALE_X, Game1.SCALE_Y), SpriteEffects.None, 1);
                    else
                        Game1.spriteBatch.Draw(texttureButtonMenu[DEF.FRAME_RETRY_HIGHTLIGHT], new Vector2(DEF.WINLOSE_BUTTON_X1, DEF.WINLOSE_BUTTON_Y1), null, Color.White, 0f, new Vector2(0, 0), new Vector2(Game1.SCALE_X, Game1.SCALE_Y), SpriteEffects.None, 1);


                    if (InputState.isTouchDrapInRect(DEF.WINLOSE_BUTTON_X2, DEF.WINLOSE_BUTTON_Y2, DEF.DIALOG_BUTTON_CONFIRM_W, DEF.DIALOG_BUTTON_CONFIRM_H))
                        Game1.spriteBatch.Draw(texttureButtonMenu[DEF.FRAME_MAINMENU_NORMAL], new Vector2(DEF.WINLOSE_BUTTON_X2, DEF.WINLOSE_BUTTON_Y2), null, Color.White, 0f, new Vector2(0, 0), new Vector2(Game1.SCALE_X, Game1.SCALE_Y), SpriteEffects.None, 1);
                    else
                        Game1.spriteBatch.Draw(texttureButtonMenu[DEF.FRAME_MAINMENU_HIGHTLIGHT], new Vector2(DEF.WINLOSE_BUTTON_X2, DEF.WINLOSE_BUTTON_Y2), null, Color.White, 0f, new Vector2(0, 0), new Vector2(Game1.SCALE_X, Game1.SCALE_Y), SpriteEffects.None, 1);

                    break;
                case MESSAGE_DTOR:
                    break;
            }
        }
    }
}